<template>
  <div class="home-container">
    <el-container class="home-container">
      <!-- 头部区域 -->
      <el-header>
        <div>
          <img src="../assets/logo.png"
               alt="" />
          <span>电商后台管理系统</span>
        </div>
        <div>
          <el-button type="info"
                     @click="hrefSurvey">调查系统</el-button>
          <el-button type="info"
                     @click="logOut">退出</el-button>
        </div>
      </el-header>
      <el-container>
        <!-- 左侧区域 -->
        <el-aside :width="collapse ? '60px' : '200px'">
          <!-- 折叠菜单区 -->
          <div class="toggle-button"
               @click="toggleCollapse">|||</div>
          <!-- 侧边栏菜单区 -->
          <el-menu background-color="#333744"
                   text-color="#fff"
                   active-text-color="#409eff"
                   unique-opened
                   :collapse="collapse"
                   :collapse-transition="false"
                   :default-active="activepath"
                   router>
            <!-- 一级菜单 -->
            <el-submenu :index="item.id + ''"
                        v-for="(item, index1) in menulist"
                        :key="item.id">
              <!-- 一级菜单的模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconObj[index1]"></i>
                <!-- 文本 -->
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item v-for="subitem in item.children"
                            :key="subitem.id"
                            :index="'/'+subitem.path"
                            @click="saveNavState('/'+subitem.path)">
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{ subitem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主题区域 -->
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  // 左侧菜单数据
  data () {
    return {
      menulist: [],
      iconObj: [
        'iconfont icon-yonghuguanli',
        'iconfont icon-quanxianguanli',
        'iconfont icon-shangpinguanli',
        'iconfont icon-dingdanguanli',
        'iconfont icon-icon-test'
      ],
      collapse: false,
      // 默认激活地址
      activepath: []
    }
  },
  // 内置创建函数
  created () {
    this.getMenulist()
    this.activepath = window.sessionStorage.getItem('activepath')
  },
  methods: {
    // 注销退出功能
    logOut () {
      // 清空会话缓存
      window.sessionStorage.clear()
      // 重定向跳转到登录页面
      this.$router.push('/login')
    },
    // 获取接口数据
    async getMenulist () {
      const { data: res } = await this.$http.get('/menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(this.menulist)
    },
    // 折叠功能
    toggleCollapse () {
      this.collapse = !this.collapse
    },
    // 保存链接的激活状态
    saveNavState (activepath) {
      window.sessionStorage.setItem('activepath', activepath)
      this.activepath = activepath
    },
    // 跳转到调查系统
    hrefSurvey () { this.$router.push('/survey') }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  padding-left: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  div {
    display: flex;
    align-items: center;
    justify-content: space-around;
    img {
      height: 10%;
      width: 10%;
      border-radius: 50%;
    }
    span {
      margin-left: 20px;
      flex: 1;
    }
  }
}
.el-aside {
  background-color: #333744;
  .toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    color: #fff;
    cursor: pointer;
  }
  .el-menu {
    border-right: none;
  }
  span {
    margin-left: 10px;
  }
}
.el-main {
  background-color: #eaedf1;
}
.home-container {
  height: 100%;
}
</style>
